﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib.Filters;
using FrameworkLib.Filters;

namespace ControlsLib
{
    [Flags]
    public enum Actions
    {
        Unselected=1,
        Add=2,
        Modify=4,
        Delete=8,
        Confirm=16,
        Cancel=32,
        New=64,
        Custom=128,
        ItemSelected=256, 
        Reset=1024
    }

    [Serializable]
    public partial class ButtonsBar<T> : UserControl where T : class
    {
        public event EventHandler<CustomEventArgs<T>> ButtonClicked;
        public event EventHandler<CustomEventArgs<T>> CustomButtonClicked;

        private T m_item;
        private Actions m_currentAction = Actions.Unselected;
        private bool m_showOnlyCustomButton = false;
        private bool m_showPager = false;
        private bool m_showFilterButton = false;

        public bool ShowFilterButton
        {
            get { return m_showFilterButton; }
            set 
            { 
                m_showFilterButton = value;

                this.filterControl1.Visible = value;
            }
        }

        public bool ShowPager 
        {
            get { return m_showPager; }
            set 
            { 
                m_showPager = value;

                this.pager11.Visible = m_showPager;
            }
        }

        public bool ShowOnlyCustomButton
        {
            get { return m_showOnlyCustomButton; }
            set 
            { 
                m_showOnlyCustomButton = value;

                ShowCustomButtons();
            }
        }

        public Actions CurrentAction
        {
            get { return m_currentAction; }
            set 
            { 
                m_currentAction = value;

                SetUpButtons();
            }
        }

        public T Item
        {
            get { return m_item; }
            set { m_item = value; }
        }

        public ButtonsBar()
        {
            InitializeComponent();

            ShowCustomButtons();

            this.btnNew.Click += new EventHandler(btnNew_Click);
            this.btnAdd.Click += new EventHandler(btnAdd_Click);
            this.btnModify.Click += new EventHandler(btnModify_Click);
            this.btnDelete.Click += new EventHandler(btnDelete_Click);
            this.btnOk.Click += new EventHandler(btnOk_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        private void ShowCustomButtons()
        {

            this.btnModify.Visible = !m_showOnlyCustomButton;
            this.btnDelete.Visible = !m_showOnlyCustomButton;
            this.btnAdd.Visible = !m_showOnlyCustomButton;
            this.btnNew.Visible = !m_showOnlyCustomButton;
        }

        #region Standard Buttons Event Handlers...
        private void btnNew_Click(object sender, EventArgs e)
        {
            m_currentAction = Actions.Add;

            if (ButtonClicked != null)
            {
                ButtonClicked(this, new CustomEventArgs<T>(Actions.New, null));
            }

            //SetUpButtons();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (ButtonClicked != null)
            {
                ButtonClicked(this, new CustomEventArgs<T>(Actions.Add, m_item));
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (ButtonClicked != null)
            {
                ButtonClicked(this, new CustomEventArgs<T>(Actions.Modify, m_item));
            }
        }

        public void btnDelete_Click(object sender, EventArgs e)
        {
            if (ButtonClicked != null)
            {
                ButtonClicked(this, new CustomEventArgs<T>(Actions.Delete, m_item));
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (ButtonClicked != null)
            {
                ButtonClicked(this, new CustomEventArgs<T>(Actions.Confirm, m_item));
            }
        }

        public void btnCancel_Click(object sender, EventArgs e)
        {
            if (ButtonClicked != null)
            {
                ButtonClicked(this, new CustomEventArgs<T>(Actions.Cancel, m_item));
            }
        }
        #endregion
        
        public void SetUpButtons()
        {
            this.btnDelete.Visible = false;
            this.btnModify.Visible = false;
            this.btnAdd.Visible = false;
            this.btnNew.Visible = false;

            if (CheckFlag(m_currentAction, Actions.New))
            {
                this.btnNew.Visible = true;
            }

            if (CheckFlag(m_currentAction, Actions.Add))
            {
                this.btnAdd.Visible = true;
            }

            if (CheckFlag(m_currentAction, Actions.Delete))
            {
                this.btnDelete.Visible = true;
            }

            if (CheckFlag(m_currentAction, Actions.Modify))
            {
                this.btnNew.Visible = true;
                this.btnModify.Visible = true;
            }

            if (CheckFlag(m_currentAction, Actions.Confirm))
            {
            }

            if (CheckFlag(m_currentAction, Actions.Cancel))
            {
            }

            if (CheckFlag(m_currentAction, Actions.Custom))
            {
                InitializeButtonBar();
            }
        }

        public void InitializeButtonBar()
        {
            if (m_showOnlyCustomButton)
            {
                this.btnModify.Visible = false;
                this.btnDelete.Visible = false;
                this.btnAdd.Visible = false;
                this.btnNew.Visible = false;
            }
        }

        private bool CheckFlag(Actions targetVal, Actions checkVal)
        {
            return ((targetVal & checkVal) == checkVal);
        }

        public void AddCustomButton(string name, string text)
        {
            Button btn = new Button() { Text = text, Name=name };
            btn.AutoSize = true;
            btn.Click += new EventHandler(btn_Click);

            this.flpCustomButtons.Controls.Add(btn);
        }

        public Button GetCustomButton(string name)
        {
            return (Button)this.flpCustomButtons.Controls[name];
        }

        public Pagers.Pager1 GetPager()
        {
            return this.pager11;
        }

        public FilterControl GetFilterControl()
        {
            return this.filterControl1;
        }

        void btn_Click(object sender, EventArgs e)
        {
            m_currentAction = Actions.Custom;

            if (CustomButtonClicked != null)
            {
                CustomButtonClicked(this, new CustomEventArgs<T>(Actions.Custom, ((Button)sender).Name, m_item));
            }
        }

        internal void EnableButton(string name, bool enable)
        {
            if (this.flpCustomButtons.Controls.ContainsKey(name))
            {
                if (this.flpCustomButtons.Controls[name] != null)
                {
                    this.flpCustomButtons.Controls[name].Enabled = enable;
                }
            }
            else
            {
                if (flpStandardButtons.Controls.ContainsKey(name))
                {
                    if (this.flpStandardButtons.Controls[name] != null)
                    {
                        this.flpStandardButtons.Controls[name].Enabled = enable;
                    }
                }
            }
        }
    }

    public class CustomEventArgs<T> : EventArgs
    {
        private T m_item;
        private Actions m_action;
        private string m_buttonName;

        public string ButtonName
        {
            get { return m_buttonName; }
            set { m_buttonName = value; }
        }

        public Actions Action
        {
            get { return m_action; }
            set { m_action = value; }
        }

        public T Item
        {
            get { return m_item; }
            set { m_item = value; }
        }

        public CustomEventArgs(Actions action, T item)
        {
            m_action = action;
            m_item = item;
        }

        public CustomEventArgs(Actions action, string buttonName, T item)
        {
            m_action = action;
            m_item = item;
            m_buttonName = buttonName;
        }
    }
}
